Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Receiving Javascript error "Uncaught SyntaxError: cannot use import statement outside a module" when trying to import. Solution found

Diving into the specifics, I am trying to import code from a JS file called GLTFLoader for Three.js. The goal is to parse a .GLB file and render a Teapot.

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        Three.js #1
    </title>
    <style>
        body{margin: 0;}
    </style>
</head>
<body>
    <script src="js/three.js"></script> // A dependecy.
    <script src="js/cube.js"></script> // The main script.
    <script src="js/loaders/GLTFLoader.mjs"></script> // The module I am trying to import code
</body>                                               // from.
</html>

The Import code and code requiring the import. Can upload full code if need be.

import {GTLFLoader} from "js/loaders/GLTFLoader.mjs"

// Create a mesh.
const loader = new GLTFLoader(); // Instantiate loader.
loader.load("assets/models/utahteapot.glb", function (gltf) { // Load the model.
scene.add(gltf.scene); // Add the loaded model to scene.
}, undefined, function (error) { // Return an error if there is one.
console.error(error);
});

I have tried using the "type="module"" tag within the script tag, I have tried uploading the Loader code straight from the url source, I have tried importing the code within the HTML file, and I even tried creating a local server to host these files. I wasn't doing that before, and I still am now. I have ran out of ideas; I can't find a solution. EDIT: Forgot to mention, the file GLTFLoader extension is .MJS because of an unrelated error I had along this error.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found a solution. It wasn't how I typed or indexed the files, it was how I was serving them on my local server.

The MIME error was caused by the files always returning as an improper MIME type "plain/text" when I needed it to return as "application/x-javascript". The import error was caused by me not adding type="module" to my main.js file within the HTML file.

So, I scouted for a python script that created a LocalHost server that served the files properly. So that meant I can label the main.js file as a module, and import what I needed from other files.

Here is the script's git.

NOTE: You may have to change your localhost number. The set number is 8080, and I had to change mine to 8090 before my files actually updated. Keep that in mind if your files aren't updating or an error is returned.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda